import request from '@/utils/request'

/**
 * 系统配置API
 */

// 获取所有系统配置列表
export function getSystemConfigs() {
  return request({
    url: '/admin/configs',
    method: 'get'
  })
}

// 通过键名获取单个配置
export function getConfigByKey(key) {
  return request({
    url: `/admin/configs/get/${key}`,
    method: 'get'
  })
}

// 获取公开配置
export function getPublicConfigs() {
  return request({
    url: '/admin/configs/public',
    method: 'get'
  })
}

// 添加系统配置
export function addConfig(data) {
  return request({
    url: '/admin/configs/add',
    method: 'post',
    data
  })
}

// 更新系统配置
export function updateConfig(key, data) {
  return request({
    url: `/admin/configs/${key}`,
    method: 'put',
    data
  })
}

// 删除系统配置
export function deleteConfig(key) {
  return request({
    url: `/admin/configs/${key}`,
    method: 'delete'
  })
}

// 批量获取配置
export function getBatchConfigs(keys) {
  return request({
    url: '/admin/configs/batch',
    method: 'get',
    params: { keys: keys.join(',') }
  })
}

// 批量更新配置
export function batchUpdateConfigs(data) {
  return request({
    url: '/admin/configs/batch',
    method: 'put',
    data
  })
}

// 重置配置
export function resetConfigs() {
  return request({
    url: '/admin/configs/reset',
    method: 'post'
  })
}

// 上传配置文件
export function uploadConfigFile(key, file, data = {}) {
  const formData = new FormData()
  formData.append('file', file)

  if (data.type) formData.append('type', data.type)
  if (data.group) formData.append('group', data.group)
  if (data.is_public !== undefined) formData.append('is_public', data.is_public)

  return request({
    url: `/admin/configs/upload/${key}`,
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
} 